package com.resou.reader.view;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.resou.reader.R;
import com.resou.reader.utils.ToastUtil;
import com.resou.reader.utils.WXUtils;
import com.resou.reader.utils.log.RsLog;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;

/**
 * Created by ${Garrett} on 2018/8/31.
 * Contact me krouky@outlook.com
 */
public class TitleLayout extends FrameLayout implements View.OnClickListener {

    private ImageView mLeftImage;
    private TextView mCenterText;
    private ImageView mRightImage;
    private Button mRightBtn;

    public TitleLayout(@NonNull Context context) {
        this(context, null);
    }

    public TitleLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TitleLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        View view = LayoutInflater.from(context).inflate(R.layout.common_title_layout, this, true);
        mLeftImage = view.findViewById(R.id.title_back);
        mCenterText = view.findViewById(R.id.title_text);
        mRightImage = view.findViewById(R.id.title_share);
        mRightBtn = view.findViewById(R.id.title_right_btn);

        TypedArray ad = context.getTheme().obtainStyledAttributes(attrs, R.styleable.TitleLayout, defStyleAttr, 0);
        int n = ad.getIndexCount();
        for (int i = 0; i < n; i++) {
            int attr = ad.getIndex(i);
            switch (attr) {
                case R.styleable.TitleLayout_leftImageSrc:
                    int src = ad.getInt(attr, 0);
                    if (src != 0) {
                        mLeftImage.setImageResource(src);
                    }
                    break;
                case R.styleable.TitleLayout_leftImageVisible:
                    if (ad.getBoolean(attr, true)) {
                        mLeftImage.setVisibility(View.VISIBLE);
                    } else {
                        mLeftImage.setVisibility(View.GONE);
                    }
                    break;
                case R.styleable.TitleLayout_centerText:
                    String str = ad.getString(attr);
                    if (!TextUtils.isEmpty(str)) {
                        mCenterText.setText(str);
                    }
                    break;
                case R.styleable.TitleLayout_centerTextVisible:
                    if (ad.getBoolean(attr, true)) {
                        mCenterText.setVisibility(View.VISIBLE);
                    } else {
                        mCenterText.setVisibility(View.GONE);
                    }
                    break;
                case R.styleable.TitleLayout_rightImageSrc:
                    int srcs = ad.getInt(attr, 0);
                    if (srcs != 0) {
                        mRightImage.setImageResource(srcs);
                    }
                    break;
                case R.styleable.TitleLayout_rightImageVisible:
                    if (ad.getBoolean(attr, true)) {
                        mRightImage.setVisibility(View.VISIBLE);
                    } else {
                        mRightImage.setVisibility(View.GONE);
                    }
                    break;
                case R.styleable.TitleLayout_rightButtonVisible:
                    if (ad.getBoolean(attr, false)) {
                        //error
                        mRightBtn.setVisibility(View.VISIBLE);
                    } else {
                        mRightBtn.setVisibility(View.GONE);
                    }
                    break;
                case R.styleable.TitleLayout_rightButtonText:
                    String rightText = ad.getString(attr);
                    if (!TextUtils.isEmpty(rightText)) {
                        mRightBtn.setText(rightText);
                    }
                    break;
                case R.styleable.TitleLayout_rightButtonBackground:
                    Drawable drawable = ad.getDrawable(attr);
                    mRightBtn.setBackground(drawable);
                default:
                    break;

            }
        }

        mRightImage.setOnClickListener(this);
        mLeftImage.setOnClickListener(this);
    }

    public void setLeftClickListener(OnClickListener l) {
        mLeftImage.setOnClickListener(l);
    }

    public void setRightClickListener(OnClickListener l) {
        mRightImage.setOnClickListener(l);
    }

    public void setCenterText(CharSequence text) {
        mCenterText.setText(text);
    }

    public void setRightText(CharSequence text) {
        mRightBtn.setText(text);
    }

    public void setRightBtnClickListener(OnClickListener l) {
        mRightBtn.setOnClickListener(l);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.title_back:
                back();
                break;
            case R.id.title_share:
                share();
                break;
        }
    }

    private void back() {
        ((Activity) getContext()).onBackPressed();
    }

    private void share() {
        new ShareAction((Activity) getContext()).withText("share test!").setDisplayList(SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE,
                SHARE_MEDIA.WEIXIN_FAVORITE, SHARE_MEDIA.QQ, SHARE_MEDIA.QZONE, SHARE_MEDIA.SINA)
                .setCallback(shareListener).open();
    }

    private UMShareListener shareListener = new UMShareListener() {
        /**
         * @descrption 分享开始的回调
         * @param platform 平台类型
         */
        @Override
        public void onStart(SHARE_MEDIA platform) {
            switch (platform) {
                case WEIXIN:
                case WEIXIN_CIRCLE:
                    if (!WXUtils.isWeixinAvilible(getContext())) {
                        ToastUtil.makeShortToast("您未安装微信，请选择其他分享方式");
                        return;
                    }
                    break;
                case QZONE:
                case QQ:
                    if (!WXUtils.isQQClientAvailable(getContext())) {
                        ToastUtil.makeShortToast("您未安装QQ，请选择其他分享方式");
                        return;
                    }
                    break;
            }
        }

        /**
         * @descrption 分享成功的回调
         * @param platform 平台类型
         */
        @Override
        public void onResult(SHARE_MEDIA platform) {
            Toast.makeText(getContext(), "成功了", Toast.LENGTH_LONG).show();
        }

        /**
         * @descrption 分享失败的回调
         * @param platform 平台类型
         * @param t 错误原因
         */
        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            Toast.makeText(getContext(), "失败" + t.getMessage(), Toast.LENGTH_LONG).show();
        }

        /**
         * @descrption 分享取消的回调
         * @param platform 平台类型
         */
        @Override
        public void onCancel(SHARE_MEDIA platform) {
            Toast.makeText(getContext(), "取消了", Toast.LENGTH_LONG).show();
        }
    };

}
